/*!
 * \file WtDtPorter.cpp
 * \project	WonderTrader
 *
 * \author Wesley
 * \date 2020/03/30
 *
 * \brief
 */
#include "WtDtPorter.h"
#include "WtDtRunner.h"

#include "../WTSTools/WTSLogger.h"
#include "../WtDtCore/WtHelper.h"

#include "../Includes/WTSVersion.h"
#include "../Share/ModuleHelper.hpp"

#ifdef _WIN32
#ifdef _WIN64
char PLATFORM_NAME[] = "X64";
#else
char PLATFORM_NAME[] = "X86";
#endif
#else
char PLATFORM_NAME[] = "UNIX";
#endif

#ifdef _MSC_VER
#include "../Common/mdump.h"
#include <boost/filesystem.hpp>
//这个主要是给MiniDumper用的
const char* getModuleName() {
    static char MODULE_NAME[250] = {0};
    if (strlen(MODULE_NAME) == 0) {
        GetModuleFileName(g_dllModule, MODULE_NAME, 250);
        boost::filesystem::path p(MODULE_NAME);
        strcpy(MODULE_NAME, p.filename().string().c_str());
    }

    return MODULE_NAME;
}
#endif

WtDtRunner& getRunner() {
    static WtDtRunner runner;
    return runner;
}

void initialize(WtString cfgFile, WtString logCfg, bool bCfgFile,
                bool bLogCfgFile) {
#ifdef _MSC_VER
    CMiniDumper::Enable(getModuleName(), true, WtHelper::get_cwd());
#endif
    getRunner().initialize(cfgFile, logCfg, getBinDir(), bCfgFile, bLogCfgFile);
}

void start(bool bAsync /* = false*/) { getRunner().start(bAsync); }

const char* get_version() {
    static std::string _ver;
    if (_ver.empty()) {
        _ver = PLATFORM_NAME;
        _ver += " ";
        _ver += WT_VERSION;
        _ver += " Build@";
        _ver += __DATE__;
        _ver += " ";
        _ver += __TIME__;
    }
    return _ver.c_str();
}

void write_log(unsigned int level, const char* message, const char* catName) {
    if (strlen(catName) > 0) {
        WTSLogger::log_raw_by_cat(catName, (WTSLogLevel)level, message);
    }
    else {
        WTSLogger::log_raw((WTSLogLevel)level, message);
    }
}

#pragma region "扩展Parser接口"

bool create_ext_parser(const char* id) {
    return getRunner().createExtParser(id);
}

void parser_push_quote(const char* id, WTSTickStruct* curTick,
                       WtUInt32 uProcFlag) {
    getRunner().on_ext_parser_quote(id, curTick, uProcFlag);
}

void register_parser_callbacks(FuncParserEvtCallback cbEvt,
                               FuncParserSubCallback cbSub) {
    getRunner().registerParserPorter(cbEvt, cbSub);
}

#pragma endregion "扩展Parser接口"

#pragma region "扩展Dumper接口"
bool create_ext_dumper(const char* id) {
    return getRunner().createExtDumper(id);
}

void register_extended_dumper(FuncDumpBars barDumper,
                              FuncDumpTicks tickDumper) {
    getRunner().registerExtDumper(barDumper, tickDumper);
}

void register_extended_hftdata_dumper(FuncDumpOrdQue ordQueDumper,
                                      FuncDumpOrdDtl ordDtlDumper,
                                      FuncDumpTrans transDumper) {
    getRunner().registerExtHftDataDumper(ordQueDumper, ordDtlDumper,
                                         transDumper);
}
#pragma endregion "扩展Dumper接口"
